<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传感器类别设置</title>
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
    <link href="assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="assets/font-awesome/css/font-awesome.min.css" rel="stylesheet"/>
    <link href="assets/ionicons-2.0.1/css/ionicons.min.css" rel="stylesheet"/>
    <link href="assets/plugins/select2/select2.min.css" rel="stylesheet"/>
    <link href="assets/AdminLTE/dist/css/AdminLTE.min.css" rel="stylesheet"/>
    <link href="assets/AdminLTE/dist/css/skins/_all-skins.min.css" rel="stylesheet"/>
    <link href="assets/vakata-jstree-7a976d1/dist/themes/default/style.min.css" rel="stylesheet"/>
    <link href="assets/images/log.ico" rel="shortcut icon">
    <link href="assets/ipicture/css/iPicture_location.css" rel="stylesheet" media="screen"/>
</head>
<body class="hold-transition skin-red sidebar-mini">
<div class="wrapper">
    <!--顶部导航栏开始-->
    <header id="top_header" class="main-header">
        <!-- Logo -->
        <a href="main_page.html" class="logo">
            <!-- mini logo for sidebar mini 50x50 pixels -->
            <span class="logo-mini"><b>T</b>pson</span>
            <!-- logo for regular state and mobile devices -->
            <span class="logo-lg"><b>T</b>PSON</span>
        </a>
        <!-- Header Navbar: style can be found in header.less -->
        <nav class="navbar navbar-static-top" role="navigation">
            <!-- Sidebar toggle button-->
            <a href="#" class="sidebar-toggle" data-toggle="offcanvas" role="button">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </a>
            <div class="navbar-custom-menu">
                <ul class="nav navbar-nav">

                    <!-- User Account: style can be found in dropdown.less -->
                    <li class="dropdown user user-menu">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                            <!--<img src="assets/dist/img/user2-160x160.jpg" class="user-image" alt="User Image">-->
                            <span class="hidden-xs">Administrator</span>
                        </a>
                    </li>
                    <!-- Control Sidebar Toggle Button -->
                </ul>
            </div>
        </nav>
    </header>
    <!--顶部导航栏结束-->
    <!--右侧导航栏开始-->
    <aside class="main-sidebar">
        <!-- sidebar: style can be found in sidebar.less -->
        <section class="sidebar">
            <!-- Sidebar user panel -->
            <!-- sidebar menu: : style can be found in sidebar.less -->
            <ul id="side_bar" class="sidebar-menu">
                <li class="header">导航栏</li>
                <li class="treeview">
                    <a href="main_page.html">
                        <i class="fa fa-dashboard text-red"></i>
                        <span>主页</span>
                    </a>
                </li>

                <li class="treeview">
                    <a href="alert_log.html">
                        <i class="fa fa-warning text-red"></i>
                        <span>报警记录</span>
                    </a>
                </li>
                <li class="treeview">
                    <a href="map_show.html">
                        <i class="fa fa-map-marker text-red"></i>
                        <span>地图展示</span>
                    </a>
                </li>
                <li class="treeview">
                    <a href="#">
                        <i class="fa fa-cog text-red"></i>
                        <span>设备管理</span>
                        <i class="fa fa-angle-left pull-right"></i>
                    </a>
                    <ul class="treeview-menu">
                        <!--<li><a href="router_status.html"><i class="fa fa-circle-o text-yellow"></i>设备状态 </a> </li>-->
                        <li><a href="router_config.html"><i class="fa fa-circle-o text-yellow"></i>设备配置 </a> </li>
                    </ul>
                </li>
                <li class="treeview active">
                    <a href="#">
                        <i class="fa fa-chain-broken text-red"></i>
                        <span>监测点配置</span>
                        <i class="fa fa-angle-left pull-right"></i>
                    </a>
                    <ul class="treeview-menu">
                        <li><a href="monitor_config_map_config.html"><i class="fa fa-circle-o text-yellow"></i>地图配置 </a> </li>
                        <li><a href="monitor_config_sensor_type_input.html"><i class="fa fa-circle-o text-red"></i>传感器类别设置 </a> </li>
                        <li><a href="monitor_config_monitor_input.html"><i class="fa fa-circle-o text-yellow"></i>监测点配置 </a> </li>
                        <li><a href="monitor_config_export.html"><i class="fa fa-circle-o text-yellow"></i>数据导入导出 </a> </li>
                    </ul>
                </li>

                <li id="sys_log" class="treeview">
                    <a href="show_log.html">
                        <i class="fa fa-files-o text-red"></i>
                        <span>系统日志</span>
                    </a>
                </li>
            </ul>
        </section>
        <!-- /.sidebar -->
    </aside>
    <!--右侧导航栏结束-->
    <!--内容区开始-->
    <div class="content-wrapper">
        <section class="content-header">
            <h1>
                <b>传感器类设置</b>
            </h1>
            <ol class="breadcrumb">
                <li><a href="main_page.html"><i class="fa fa-dashboard"></i> 主页</a></li>
                <li><a href="#"><i class="fa fa-chain-broken"></i> 监测点配置</a> </li>
                <li><a href="monitor_config_sensor_type_input.html"><i class="fa fa-circle-o"></i> 传感器类别设置</a></li>
            </ol>
        </section>
        <section class="invoice">
            <div class="row">
                <div class="col-md-3">
                    <div class="box box-warning" >
                        <div class="box-header">
                            <h4 class="box-title"><b>传感器类别列表</b></h4>
                        </div>
                        <div class="box-body">
                            <pre class="prettyprint" style="border: none;height: 700px;">
                                <div id="sensor_tree"></div>
                            </pre>
                        </div>
                    </div>
                </div>
                <div class="col-md-9">
                    <div id="sensor_setting" class="box box-success">
                        <div class="box-header">
                            <h4 class="box-title"><b>传感器类别设置</b></h4>
                        </div>
                        <div class="box-body">
                            <div class="nav-tabs-custom">
                                <ul class="nav nav-tabs">
                                    <li class="active"><a href="#tab_1" data-toggle="tab">录入数据</a> </li>
                                    <li><a href="#tab_2" data-toggle="tab">修改数据</a> </li>
                                    <li><a href="#tab_3" data-toggle="tab">删除数据</a> </li>
                                </ul>
                                <div class="tab-content">
                                    <div class="tab-pane active" id="tab_1">
                                        <div class="row">
                                            <div class="pad margin no-print">
                                                <div class="callout callout-info" style="margin-bottom: 0!important;">
                                                    从左侧选择一级、然后录入二级菜单数据，此时三级菜单不可用。<br>
                                                    从左侧选择一级、二级菜单，然后录入三级菜单。
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="box box-default">
                                                <div class="box-header">
                                                    <h4 class="box-title">一级目录录入</h4>
                                                </div>
                                                <div class="box-body">
                                                    <div class="row">
                                                        <div class="col-sm-12">
                                                            <div class="col-sm-6">
                                                                <div class="input-group">
                                                                    <span class="input-group-addon"><i class="glyphicon glyphicon-briefcase text-yellow"></i></span>
                                                                    <input id="level_1_input" type="text" class="form-control" placeholder="一级菜单">
                                                                </div>
                                                            </div>
                                                            <div class="col-sm-3 col-sm-offset-3">
                                                                <button id="levelOneSave" type="button" class="btn btn-success btn-block glyphicon glyphicon-saved pull-right"> 保存</button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="box box-default">
                                                <div class="box-header">
                                                    <h4 class="box-title">二级目录录入</h4>
                                                </div>
                                                <div class="box-body">
                                                    <div class="row">
                                                        <div class="col-sm-12" id="level2_begin_input">
                                                            <div class="col-sm-3">
                                                                <div class="form-group">
                                                                    <label>一级项目选择</label>
                                                                    <select id="tab_1_level_2_select_1" class="form-control select2 tab_1_2_level_1_select" style="width: 100%"></select>
                                                                </div>
                                                            </div>
                                                            <div class="col-sm-3">
                                                                <div class="form-group">
                                                                    <label>二级项目录入</label>
                                                                    <div class="input-group">
                                                                        <span class="input-group-addon"><i class="glyphicon glyphicon-folder-open text-yellow"></i></span>
                                                                        <input id="tab_1_level_2_input" type="text" class="form-control">
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <div class="col-sm-3 col-sm-offset-3">
                                                                <button id="saveBtnTabInputLevel2" type="button" class="btn btn-success btn-block glyphicon glyphicon-saved"
                                                                        style="margin-top: 24px"> 保存</button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="box box-default">
                                                <div class="box-header">
                                                    <h4 class="box-title">三级目录录入</h4>
                                                </div>
                                                <div class="box-body">
                                                    <div class="row">
                                                        <div class="col-sm-12" id="level3_begin_input">
                                                            <div class="col-sm-3">
                                                                <div class="form-group">
                                                                    <label>一级项目选择</label>
                                                                    <select id="tab_1_level_3_select_1" class="form-control select2 tab_1_3_level_1_select" style="width: 100%"></select>
                                                                </div>
                                                            </div>
                                                            <div class="col-sm-3">
                                                                <div class="form-group">
                                                                    <label>二级项目选择</label>
                                                                    <select id="tab_1_level_3_select_2" class="form-control select2 tab_1_3_level_2_select" style="width: 100%"></select>
                                                                </div>
                                                            </div>
                                                            <div class="col-sm-3">
                                                                <div class="form-group">
                                                                    <label>三级项目录入</label>
                                                                    <div class="input-group">
                                                                        <span class="input-group-addon"><i class="glyphicon glyphicon-file text-yellow"></i></span>
                                                                        <input id="tab_1_level_3_input" type="text" class="form-control">
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <div class="col-sm-3">
                                                                <button id="saveBtnTabInputLevel3" type="button" class="btn btn-success btn-block glyphicon glyphicon-saved"
                                                                        style="margin-top: 24px"> 保存</button>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="tab-pane" id="tab_2">
                                        <div class="row">
                                            <div class="pad margin no-print">
                                                <div class="callout callout-warning" style="margin-bottom: 0!important;">
                                                    单击左侧树状表中的内容，然后修改文本框中的内容，最后提交。
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-sm-12">
                                                <div class="col-sm-2">
                                                    <div class="input-group">
                                                        <span class="input-group-addon"><i class="fa fa-key text-yellow"></i></span>
                                                        <input id="tree_click_id" type="text" class="form-control" disabled>
                                                    </div>
                                                </div>
                                                <div class="col-sm-4">
                                                    <div class="input-group">
                                                        <span class="input-group-addon"><i class="fa  fa-file-text text-yellow"></i></span>
                                                        <input id="tree_click_text" type="text" class="form-control" >
                                                    </div>
                                                </div>
                                                <div class="col-sm-3 col-sm-offset-3">
                                                    <button id="saveChangeBtn" type="button" class="btn btn-success glyphicon glyphicon-saved btn-block"> 保存</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="tab-pane" id="tab_3">
                                        <div class="row">
                                            <div class="pad margin no-print">
                                                <div class="callout callout-danger" style="margin-bottom: 0!important;">
                                                    单击左侧树状表中的内容，然后执行删除操作。此过程不可逆！
                                                </div>
                                            </div>
                                        </div>
                                        <div class="row">
                                            <div class="col-sm-12">
                                                <div class="col-sm-2">
                                                    <div class="input-group">
                                                        <span class="input-group-addon"><i class="fa fa-key text-red"></i></span>
                                                        <input id="tree_click_id_del" type="text" class="form-control" disabled>
                                                    </div>
                                                </div>
                                                <div class="col-sm-4">
                                                    <div class="input-group">
                                                        <span class="input-group-addon"><i class="fa  fa-file-text text-red"></i></span>
                                                        <input id="tree_click_text_del" type="text" class="form-control" disabled>
                                                    </div>
                                                </div>
                                                <div class="col-sm-3 col-sm-offset-3">
                                                    <button id="delDataBtn" type="button" class="btn btn-danger glyphicon glyphicon-remove btn-block"> 删除</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        <div class="clearfix"></div>
    </div><!-- /.content-wrapper -->
    <!--内容区结束-->
    <!--页脚开始-->
    <footer class="main-footer no-print">
        <div class="pull-right hidden-xs">
            <b>Version</b>  3.1.4
        </div>
        <strong>Copyright &copy; 2015-2016 <a href="http://www.tpson.cn/" target="_blank">拓深科技</a>.</strong> All rights reserved.
    </footer>
    <!--页脚结束-->
</div>

<script src="assets/plugins/jQuery/jQuery-2.1.4.min.js"></script>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/plugins/fastclick/fastclick.min.js"></script>
<script src="assets/dist/js/app.min.js"></script>
<script src="assets/dist/js/demo.js"></script>
<script src="assets/vakata-jstree-7a976d1/dist/jstree.min.js"></script>
<script src="assets/cxSelect-master/js/jquery.cxselect.min.js"></script>
<script src="assets/layer-v2.2/layer/layer.js"></script>
<script src="assets/ipicture/js/jquery.ipicture_location.js"></script>
<script src="js/alert_check.js"></script>
<script>
    var server;
    var treeData = [];
    $(function () {
        var path = document.location.pathname;
        var href = document.location.href;
        if(path =='/'){
            var index = href.indexOf(path,href.indexOf(path)+2);
        }else {
            var index = href.indexOf(path);
        }
        var localPath = href.substring(0,index);
        var localName = path.substring(0,path.substr(1).indexOf('/')+1);
        server = localPath + localName;
        socketInit(server.substring(5));
        clear();
        getTree();
        window.onload = checkAlert(server);
    });
    function getTree() {
        $.ajax({
            url:server + "/xk/xkSensor/getTree",
            async:false,
            type:"GET",
            success:function (data_in) {
                var jsonObj = JSON.parse(data_in);
                console.log(jsonObj);
                treeData = [];
                for(var i in jsonObj.data){
                    var level1Data = jsonObj.data;
                    if(i == 0){
                        var level1Obj = {
                            //id,parentid,菜单等级
                            id: [level1Data[i].id, level1Data[i].parentId],
                            text: level1Data[i].name,
                            icon: "glyphicon glyphicon-briefcase text-yellow",
                            state: {"opened" : true }
                        };
                    }else{
                        var level1Obj = {
                            //id,parentid,菜单等级
                            id: [level1Data[i].id, level1Data[i].parentId],
                            text: level1Data[i].name,
                            icon: "glyphicon glyphicon-briefcase text-yellow",
                            state: {"opened" : false }
                        };
                    }
                    if(level1Data[i].children != undefined){
                        var level2Data = level1Data[i].children;
                        var level2Arr = [];
                        for(var j in level2Data){
                            if(i == 0 && j == 0){
                                var level2Obj = {
                                    text: level2Data[j].name,
                                    id: [level2Data[j].id,level2Data[j].parentId],
                                    icon : "glyphicon glyphicon-folder-open text-yellow",
                                    state: {"opened" : true }
                                };
                            }else{
                                var level2Obj = {
                                    text: level2Data[j].name,
                                    id: [level2Data[j].id,level2Data[j].parentId],
                                    icon : "glyphicon glyphicon-folder-open text-yellow",
                                    state: {"opened" : false }
                                };
                            }

                            if(level2Data[j].children != undefined)
                            {
                                var level3Data = level2Data[j].children;
                                var level3Arr = [];
                                for(var k in level3Data){
                                    var level3Obj = {
                                        text: level3Data[k].name,
                                        id: [level3Data[k].id,level3Data[k].parentId],
                                        icon : "glyphicon glyphicon-file text-yellow",
                                    };
                                    level3Arr.push(level3Obj);
                                }
                                level2Obj.children = level3Arr;
                            }
                            level2Arr.push(level2Obj);
                        }
                        level1Obj.children = level2Arr;
                    }
                    treeData.push(level1Obj);
                }
                $('#sensor_tree').data('jstree',false).empty().jstree({
                    'plugins':["massload"],
                    'core' : {
                        'data' : treeData
                    },

                });
            }
        });
        $('#level3_begin_input').cxSelect({
            selects: ['tab_1_3_level_1_select', 'tab_1_3_level_2_select'],
            jsonName: 'text',
            jsonValue: 'id',
            jsonSub: 'children',
            data:treeData
        });
        $('#level2_begin_input').cxSelect({
            selects: ['tab_1_2_level_1_select'],
            jsonName: 'text',
            jsonValue: 'id',
            data:treeData
        });

        $('#sensor_tree').on("select_node.jstree",function (e, data) {
            var ID = data.node.id.split(',')[0];
            var text = data.node.text;
            $('#tree_click_id').val(ID);
            $('#tree_click_text').val(text);
            $('#tree_click_id_del').val(ID);
            $('#tree_click_text_del').val(text);
        });
    };
    function clear() {
        $('#sensor_tree').empty();
    };

    $('#saveBtnTabInputLevel3').click(function () {
     if($('#tab_1_level_3_select_1').get(0).selectedIndex != 0){
         if($('#tab_1_level_3_select_2').val() != null && $('#tab_1_level_3_select_2').get(0).selectedIndex != 0){
             var level2 = $('#tab_1_level_3_select_2').val();
             if($('#tab_1_level_3_input').val()[0] != null){
                 var name = $('#tab_1_level_3_input').val();
                 var parentId = level2.split(',')[0];
                 var formDatas = new FormData();
                 formDatas.append("name",name);
                 formDatas.append("parent_id",parentId);
                 formDatas.append("level",3);
                 $.ajax({
                     type:"post",
                     contentType : false,
                     processData:false,
                     url:server + '/xk/xkSensor/add',
                     async:false,
                     data:formDatas,
                     success:function (data) {
                         if(JSON.parse(data).code == 0){
                             $('#tab_1_level_3_input').val("");
                                clear();
                                getTree();
                             layer.alert('数据录入成功！',{
                                 closeBtn: 0
                             });
                         }
                         else{
                             layer.alert('操作失败：' + data,{
                                 closeBtn: 0
                             });
                         }
                     }
                 });
             }
         }
     }
    });

    $('#saveBtnTabInputLevel2').click(function () {
        if($('#tab_1_level_2_select_1').get(0).selectedIndex != 0){
            if($('#tab_1_level_2_input').val()[0] != null){
                var level1 = $('#tab_1_level_2_select_1').val();
                var name = $('#tab_1_level_2_input').val();
                var parentId = level1.split(',')[0];
                var formDatas = new FormData();
                formDatas.append("name",name);
                formDatas.append("parent_id",parentId);
                formDatas.append("level",2);
                $.ajax({
                    type:"post",
                    contentType : false,
                    processData:false,
                    url:server + '/xk/xkSensor/add',
                    async:false,
                    data:formDatas,
                    success:function (data) {
                        if(JSON.parse(data).code == 0){
                            $('#tab_1_level_2_input').val("");
                            clear();
                            getTree();
                            layer.alert('数据录入成功！',{
                                closeBtn: 0
                            });
                        }
                        else{
                            layer.alert('操作失败：' + data,{
                                closeBtn: 0
                            });
                        }
                    }
                });
            }
        }
    });

    //一级项保存
    $('#levelOneSave').click(function () {
        var name = $('#level_1_input').val();
        var formDatas = new FormData();
        formDatas.append("name",name);
        formDatas.append("parent_id",0);
        formDatas.append("level",1);
        if(name != ""){
            $.ajax({
                type:"post",
                contentType : false,
                processData:false,
                url: server + "/xk/xkSensor/add",
                data:formDatas,
                success:function (data) {
                    if(JSON.parse(data).code == 0){
                        $('#level_1_input').val("");
                        clear();
                        getTree();
                        layer.alert('数据录入成功！',{
                            closeBtn: 0
                        });
                    }
                    else{
                        layer.alert('操作失败：' + data,{
                            closeBtn: 0
                        });
                    }
                }
            });
        }else{
            layer.alert('请输入数据！',{
                closeBtn: 0
            });
        }
    });

    $('#saveChangeBtn').click(function () {
        var ID = $('#tree_click_id').val();
        var name = $('#tree_click_text').val();
        var formDatas = new FormData();
        formDatas.append("id",ID);
        formDatas.append("name",name);
        if(ID && name){
            $.ajax({
                type:"post",
                contentType : false,
                processData:false,
                url:server + '/xk/xkSensor/updateName',
                data:formDatas,
                success:function (data) {
                    if(JSON.parse(data).code == 0){
                        clear();
                        getTree();
                        layer.alert('修改成功！',{
                            closeBtn: 0
                        });
                    }
                    else{
                        layer.alert('操作失败：' + data,{
                            closeBtn: 0
                        });
                    }
                }
            });
        }else{
            layer.alert('请先点选树状图内的目标和输入要修改的内容。',{
                closeBtn: 0
            });
        }

    });

    $('#delDataBtn').click(function () {
        var ID = $('#tree_click_id_del').val();
        if(ID != null){
            $.ajax({
                url:server + '/xk/xkSensor/delete?id='+ ID,
                success:function (data) {
                    if(JSON.parse(data).code == 0){
                        $('#tree_click_id_del').val("");
                        $('#tree_click_text_del').val("");
                        clear();
                        getTree();
                        layer.alert('删除成功！',{
                            closeBtn: 0
                        });
                    }
                    else{
                        layer.alert('操作失败：' + data,{
                            closeBtn: 0
                        });
                    }
                }
            });
        }
        else{
            layer.alert('请先点选树状图内的目标。',{
                closeBtn: 0
            });
        }
    });
</script>
</body>
</html>